﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace zdjecia
{
    public partial class Form1 : Form
    {
        #region zmienne
 
        KinectZdjecia kinect;
        Boolean stop = false;
        FileInfo[] fi = null;
        int i = 0;

        #endregion

        public Form1()
        {
            InitializeComponent();

            System.Console.WriteLine("Skalibruj");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo("zdjecia");
            fi = di.GetFiles("*.jpg");

            timer1.Start();

            kinect = new KinectZdjecia("config.xml");
            kinect.Initialize();
            kinect.SwipeLeft += new KinectZdjecia.SwipeLeftHandler(kinect_SwipeLeft);
            kinect.SwipeRight += new KinectZdjecia.SwipeRightHandler(kinect_SwipeRight);
            kinect.ImageUpdated += new KinectZdjecia.ImageUpdatedHandler(kinect_ImageUpdated);

            pictureBox1.Image = Image.FromFile(fi[0].FullName);

            numericUpDown1.Value = (decimal)kinect._swipe.XAngleThreshold;
            numericUpDown2.Value = (decimal)kinect._swipe.SteadyMaximumVelocity;
            numericUpDown3.Value = (decimal)kinect._swipe.SteadyDuration;
            numericUpDown4.Value = (decimal)kinect._swipe.MinimumVelocity;
            numericUpDown5.Value = (decimal)kinect._swipe.Duration;

            checkBox1.Checked = kinect._swipe.UseSteady;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            stop = false;
        }

        #region zdarzenia

        private void kinect_SwipeLeft(object o, HandPositionEventArgs e)
        {
            if (i > 0)
                BeginInvoke(new KinectZdjecia.SwipeLeftHandler(kinect_SwipeLeft_Invoke), new object[] { o, e });
        }

        private void kinect_SwipeLeft_Invoke(object o, HandPositionEventArgs e)
        {

            if (!stop)
            {
                if (i == 0)
                    i = fi.Length - 1;
                else
                    --i;

                Image tmp = Image.FromFile(fi[i].FullName);
                Graphics g = Graphics.FromImage(tmp);

                Font font = new Font("Arial", 300);
                Brush brush = new SolidBrush(Color.Black);
                g.DrawString(i.ToString(), font, brush, 0.0F, 0.0F);

                pictureBox1.Image = tmp;

                timer1.Start();
                stop = true;
            }


        }

        private void kinect_SwipeRight(object o, HandPositionEventArgs e)
        {
            BeginInvoke(new KinectZdjecia.SwipeLeftHandler(kinect_SwipeRight_Invoke), new object[] { o, e });

        }

        private void kinect_SwipeRight_Invoke(object o, HandPositionEventArgs e)
        {

            if (!stop)
            {
                i = ++i % fi.Length;

                Image tmp = Image.FromFile(fi[i].FullName);
                Graphics g = Graphics.FromImage(tmp);

                Font font = new Font("Arial", 300);
                Brush brush = new SolidBrush(Color.Black);
                g.DrawString(i.ToString(), font, brush, 0.0F, 0.0F);

                pictureBox1.Image = tmp;
                timer1.Start();
                stop = true;
            }

        }

        private void kinect_ImageUpdated(object o, HandPositionEventArgs e)
        {
            BeginInvoke(new KinectZdjecia.ImageUpdatedHandler(kinect_ImageUpdated_Invoke), new object[] { o, e });
        }

        private void kinect_ImageUpdated_Invoke(object o, HandPositionEventArgs e)
        {
            pictureBox2.Image = kinect.b2;
        }


        #endregion

        #region zmiana_parametrow

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            kinect._swipe.XAngleThreshold = (float)numericUpDown1.Value;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            kinect._swipe.SteadyMaximumVelocity = (float)numericUpDown2.Value;
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            kinect._swipe.SteadyDuration = (uint)numericUpDown3.Value;
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            kinect._swipe.MinimumVelocity = (float)numericUpDown4.Value;
        }

        private void numericUpDown5_ValueChanged(object sender, EventArgs e)
        {
            kinect._swipe.Duration = (uint)numericUpDown5.Value;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            kinect._swipe.UseSteady = checkBox1.Checked;
        }

        #endregion
        
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            kinect.koniec();
        }

    }
}
